< previous page page_233 next page >

Page 233
Keeping the Procedure Private
Using the Private keyword in the Sub statement lets the procedure be accessed from only the form or module in which it is defined. This approach, of course, poses advantages and disadvantages. One advantage is that a private procedure is resident in memory only while the module in which it is stored is loaded, conserving system resources. A disadvantage is that the procedure is not accessible from other modules.
When working with event procedures in other chapters, you might have noticed that they are, by default, private procedures. They are private because, typically, controls are not accessed outside the form on which they reside. This is an example of information hiding, or encapsulation, a technique used in object-oriented programming. If you are sharing a module with a team of developers, you could define the functions they call as public, while the internal procedures they don't need to know about remain private.
Preserving Variables
Typically, when a procedure is executed, the variables it uses are created, used in the procedure, and then destroyed when the procedure is terminated. However, sometimes you might want to preserve the values of the variables for future calls to the procedure. You can handle this task by using the Static keyword. This keyword can be applied to the declaration of the variables in the procedure or in the declaration of the procedure itself.
When Static is used in a variable declaration, only the variables included in the Static statement are preserved. If you use the Static keyword in the procedure declaration, all the variables in the procedure are preserved. For example, a procedure that prints a report header on the printer may keep track of the current page number. In the following code example, the value of the static variable nPageNum is retained each time the procedure is run:
Public Sub PrintHeader()
    Static nPageNum As Integer
    nPageNum = nPageNum + 1
    Printer.Print "FORTNER MASONRY ANNUAL EARNINGS REPORT"
    Printer.Print Date
    Printer.Print "Page " & nPageNum
    Printer.Print
End Sub
tip.gif
For efficiency's sake, it's important to place your procedures in the appropriate scope. Giving a procedure a scope that is too broad (for example, making a procedure public when it only needs to be private) wastes valuable system resources. If you create a public procedure, Visual Basic must allocate appropriate resources to make it available to all parts of your program. Using the Static keyword to force a procedure to "remember" its local variables causes an extra allocation of resources as well. In general, you should make procedures private if possible, and avoid the use of static variables as well. If you use this approach, Visual Basic can manage memory more efficiently because it is free to unload the various sections of code as needed.

Reusing Functions and Procedures
You can create a procedure in either of two places: a form or a module. Where you place the procedure depends on where you need to use it and what its purpose is. If the procedure is

 
< previous page page_233 next page >